import 'dart:convert';

import 'package:flutter_http_utils/src/http_utils.dart';

///基础的数据格式
class HttpResponse {
  int code = 1; //：0 正常  1：错误
  String msg = ""; //信息
  // 保存原始JSON数据
  Map<String, dynamic> originalJson = {};

  HttpResponse();

  factory HttpResponse.asT(Map<String, dynamic> json) {
    return HttpResponse().fromJson(json);
  }

  HttpResponse fromJson(Map<String, dynamic> json) {
    originalJson = json; // 保存原始数据
    if (json['code'] != null) {
      code = json['code'] is String ? int.tryParse(json['code']) : json['code'].toInt();
    }
    if (json['message'] != null) {
      msg = json['message'].toString();
    }
    return this;
  }

  Map<String, dynamic> toJson() {
    if (originalJson.isEmpty) {
      final Map<String, dynamic> data = <String, dynamic>{};
      data['code'] = code;
      data['msg'] = msg;
      return data;
    } else {
      return originalJson;
    }
  }

  bool get isSucceed => code == 10200;

  // 添加获取任意字段的方法,支持 key.key1.key2
  dynamic get(String key) {
    if (key.contains('.')) {
      final List<String> keys = key.split('.');
      return getX(keys);
    }
    return originalJson[key];
  }

  // 多级获取数据
  dynamic getX(List<String> keys) {
    dynamic currentData = originalJson;
    for (final key in keys) {
      if (currentData == null) {
        return null;
      }
      if (currentData is Map && currentData.containsKey(key)) {
        currentData = currentData[key];
      } else {
        return null;
      }
    }
    return currentData;
  }

  // 可选：通过操作符[]访问字段
  dynamic operator [](String key) => get(key);

  // 获取任意字段的泛型方法
  M? getAsT<M>(String key) {
    return HttpUtils.fromJsonAsT<M>(get(key));
  }

  // 获取任意字段的泛型方法
  M? getXAsT<M>(List<String> keys) {
    return HttpUtils.fromJsonAsT<M>(getX(keys));
  }

  @override
  String toString() {
    if (originalJson.isEmpty) {
      return """
    HttpResponse{
      code=$code,
      msg=$msg,
    }
    """;
    } else {
      return """
      HttpResponse{
       $originalJson
      }
    """;
    }
  }
}

class HttpResult<T> extends HttpResponse {
  T? data;

  T get dataX => data!;

  HttpResult();

  factory HttpResult.asT(Map<String, dynamic> json) {
    return HttpResult<T>().fromJson(json);
  }

  HttpResult<T> superFromJson(Map<String, dynamic> json) {
    super.fromJson(json);
    return this;
  }

  @override
  HttpResult<T> fromJson(Map<String, dynamic> json) {
    super.fromJson(json);
    if (json["data"] != null) {
      data = HttpUtils.fromJsonAsT<T>(json['data']) as T;
    } else if (json["list"] != null) {
      data = HttpUtils.fromJsonAsT<T>(json['list']) as T;
    }
    return this;
  }

  @override
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> result = super.toJson();
    result['data'] = jsonEncode(data);
    return result;
  }

  bool get isSucceedData => isSucceed && data != null;

  @override
  String toString() {
    return """
      HttpResult{
        code: $code,
        msg: $msg,
        data: $data
      }
    """;
  }
}
//
// class HttpPageResult<T> extends HttpResult<T> {
//   /// 当前是服务器数据的第几页
//   int current = 1;
//
//   /// 一共多少页
//   int pages = -1;
//
//   ///总数
//   int total = -1;
//
//   HttpPageResult();
//
//   factory HttpPageResult.asT(Map<String, dynamic> json) {
//     return HttpPageResult<T>().fromJson(json);
//   }
//
//   @override
//   HttpPageResult<T> fromJson(Map<String, dynamic> json) {
//     super.fromJson(json);
//     if (json["current"] != null) {
//       current = json['current'] is String ? int.tryParse(json['current']) : json['current'].toInt();
//     }
//     if (json["pages"] != null) {
//       pages = json['pages'] is String ? int.tryParse(json['pages']) : json['pages'].toInt();
//     }
//     if (json["total"] != null) {
//       total = json['total'] is String ? int.tryParse(json['total']) : json['total'].toInt();
//     }
//     return this;
//   }
//
//   @override
//   Map<String, dynamic> toJson() {
//     final Map<String, dynamic> data = super.toJson();
//     data['current'] = current;
//     data['pages'] = pages;
//     data['total'] = total;
//     return data;
//   }
//
//   @override
//   String toString() {
//     return """
//       HttpPageResult{
//         code: $code,
//         msg: $msg,
//         current: $current,
//         pages: $pages,
//         total: $total,
//         data: $data
//       }
//     """;
//   }
// }
